<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>tab选项卡切换</title>
    <style>
        .tab {
            height: 500px;
            width: 900px;
            border: 1px solid black;
            margin: 0 auto;
            margin-top: 200px;
            display: flex;
            flex-direction: column;
        }

        .menu {
            height: 100px;
            width: 900px;
            border-bottom: 1px solid black;
            display: flex;
        }

        .name {
            height: 100px;
            width: 300px;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid black;
            font-size: 30px;
        }

        .content {
            height: 400px;
            width: 900px;
            border: 1px solid black;
            font-size: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .species {
            display: none;
        }

        .selected {
            display: block;
        }

        .active {
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="tab">
        <div class="menu">
            <div class="name active">艾伦</div>
            <div class="name">弗洛克</div>
            <div class="name">利威尔</div>
        </div>
        <div class="content">
            <div class="species selected">巨人</div>
            <div class="species">人类</div>
            <div class="species">阿克曼</div>
        </div>
    </div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.7.2/axios.js"></script>
<script>
    // 1. 找到li节点
    let namEle = document.querySelectorAll(".name");
    let speEle = document.querySelectorAll(".species");
    // 2. 循环数组中的每个节点，给他们单击事件
    [...namEle].forEach((naEle, index) => {
        naEle.addEventListener("click", function () {
            // 这里必须是普通函数，this才会指向当前调用者，
            // 不能是箭头函数（不绑定this）
            // 3. 排他思想：
            // 先把所有的元素移除类名active
            [...namEle].forEach((ele) => {
                ele.classList.remove("active");
            });
            // 后给当前点击的元素添加类名active
            this.classList.add("active");
            [...speEle].forEach((ele) => {
                ele.classList.remove("selected")
            });
            // 通过点击的相应下标找到div添加类名selected
            [...speEle][index].classList.add("selected");
        });
    });
</script>

</html>